
package com.joolun.cloud.weixin.admin.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.joolun.cloud.common.core.constant.CommonConstants;
import com.joolun.cloud.common.core.util.R;
import com.joolun.cloud.common.core.util.SensitiveUtils;
import com.joolun.cloud.common.log.annotation.SysLog;
import com.joolun.cloud.common.nacos.util.NacosConfigUtils;
import com.joolun.cloud.weixin.admin.config.open.WxComponentConfigProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.Yaml;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信第三方平台配置
 *
 * @author www.joolun.com
 * @date 2020-11-24 14:44:15
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/wxcomponentconfig")
@Api(value = "wxComponentConfig", tags = "微信第三方平台配置管理")
public class WxComponentConfigController {

	private NacosConfigUtils nacosConfigUtils;

    /**
     * 微信第三方平台配置查询
     * @param
     * @return R
     */
    @ApiOperation(value = "微信第三方平台配置查询")
    @GetMapping
    @PreAuthorize("@ato.hasAuthority('wx:wxcomponentconfig:get')")
    public R getBy() throws NacosException {
		ConfigService configService = nacosConfigUtils.getConfigService();
		String content = configService.getConfig(CommonConstants.CONFIG_DATA_ID_JOOLUN_WEIXIN_ADMIN, CommonConstants.CONFIG_GROUP, CommonConstants.CONFIG_TIMEOUT_MS);
		Yaml yaml = new Yaml();
		Map<String,Map> confMap= yaml.load(content);
		Object object = confMap.get("wx").get("component");
		JSONObject jSONObject = JSONUtil.parseObj(object);
		WxComponentConfigProperties wxComponentConfigProperties = jSONObject.toBean(WxComponentConfigProperties.class);
		//数据脱敏
		String sensitiveAppSecret = SensitiveUtils.process(wxComponentConfigProperties.getAppSecret());
		wxComponentConfigProperties.setAppSecret(sensitiveAppSecret);
		String sensitiveAesKey = SensitiveUtils.process(wxComponentConfigProperties.getAesKey());
		wxComponentConfigProperties.setAesKey(sensitiveAesKey);
		return R.ok(wxComponentConfigProperties);
    }

    /**
     * 微信第三方平台配置修改
     * @param wxComponentConfigProperties 微信第三方平台配置
     * @return R
     */
    @ApiOperation(value = "微信第三方平台配置修改")
    @SysLog("修改微信第三方平台配置")
    @PutMapping
    @PreAuthorize("@ato.hasAuthority('wx:wxcomponentconfig:edit')")
    public R updateById(@RequestBody WxComponentConfigProperties wxComponentConfigProperties) throws NacosException {
		ConfigService configService = nacosConfigUtils.getConfigService();
		String content = configService.getConfig(CommonConstants.CONFIG_DATA_ID_JOOLUN_WEIXIN_ADMIN, CommonConstants.CONFIG_GROUP, CommonConstants.CONFIG_TIMEOUT_MS);
		Yaml yaml = new Yaml();
		Map<String,Map> confMap= yaml.load(content);
		Map<String,Map> baseMap= confMap.get("wx");

		String appSecret = baseMap.get("component").get("appSecret")+"";
		String sensitiveAppSecret = SensitiveUtils.process(appSecret);
		if(!sensitiveAppSecret.equals(wxComponentConfigProperties.getAppSecret())){
			appSecret = wxComponentConfigProperties.getAppSecret();
		}
		String aesKey = baseMap.get("component").get("aesKey")+"";
		String sensitiveAesKey = SensitiveUtils.process(aesKey);
		if(!sensitiveAesKey.equals(wxComponentConfigProperties.getAesKey())){
			aesKey = wxComponentConfigProperties.getAesKey();
		}


		Map wxMap = new HashMap();
		wxMap.put("appId",wxComponentConfigProperties.getAppId());
		wxMap.put("appSecret",appSecret);
		wxMap.put("token",wxComponentConfigProperties.getToken());
		wxMap.put("aesKey",aesKey);
		baseMap.put("component",wxMap);
		confMap.put("wx",baseMap);
		String confStr = yaml.dumpAsMap(confMap);
		boolean rs = configService.publishConfig(CommonConstants.CONFIG_DATA_ID_JOOLUN_WEIXIN_ADMIN, CommonConstants.CONFIG_GROUP,confStr,"yaml");
        return R.ok(rs);
    }

}
